import { IFeidaoAiNodejsComponent } from '@feidao/web/interfaces';

import render from '@feidao/web/atom/nodejs/render/render';
import nodejs from '@feidao/web/atom/nodejs/msg-b/nodejs';

import server_render from '../atom-n/server-render';
import { Params } from '../atom/params2query';
import p2q from '../atom/params2query';
import generate_query4render from '../atom/generate-query4render';
import generate_sort_fields from '../atom/generate-sort-fields';

import P01 from './P01';
import P02 from './P02';
import P04 from './P04';
import P03 from './P03';
import P05 from './P05';

const name2title = {
	'staff_name': '员工名称',
	'deptname': '部门名称',
	'staff_type': '员工类型',
	'leave_time': '离岗时间',
	'companyname': '创立公司名称',
	'belong_industry': '所属行业',
	'company_address': '公司地址',
	'company_valuation': '公司估值',
	'company_scale': '公司规模'
};

function create_data_sort(fd: IFeidaoAiNodejsComponent) {
	try {
		const fields = ['staff_name', 'deptname', 'staff_type', 'leave_time', 'companyname', 'belong_industry', 'company_address', 'company_valuation', 'company_scale'];
		const params = fd.data.params;
		delete params['page-no'];
		const data = generate_sort_fields(fd.data.url, fd.data.params as Params, fields);
		data.forEach((d) => {
			const field = d.field;
			render(fd.data.node, d, P02, field);
		});
	} catch (err) {
		console.error('------err', err);
	}
}

export default async function nba001(fd: IFeidaoAiNodejsComponent, ...args: any[]) {
	let page_no = fd.data.params['page-no'];
	if (!page_no) {
		page_no = 1;
	}
	let page_size = fd.data.params['page_size'];
	if (!page_size) {
		page_size = 20;
	}
	const q = p2q(fd.data.params as Params);
	const query = {
		filter: q.criteria,
		sort: q.sort,
		page_no,
		page_size
	};
	const data = generate_query4render(fd.data.url, fd.data.params as Params, name2title);
	render(fd.data.node, data, P01, 'P01');

	// 排序
	create_data_sort(fd);
	// 没有数据
	const res = await nodejs(fd.data.spaceid, "leavepost_work_find", query);
	// 无数据渲染P04
	if (res.dataArray.length == 0) {
		server_render(fd.data.node, 'P04', P04, '');
	} else {
		server_render(fd.data.node, 'P03', P03, res.dataArray);
	}

	res['page_size'] = page_size;
	render(fd.data.node, res, P05, 'P05');
}
// leavepost_work   离岗创业
// {
// 	"_id":"id",
// 	"no":"编号",
// 	"staff_name":"员工名称",
// 	"staff_number":"员工编号",
// 	"staff_type":"员工类型",
// 	"deptid":"部门编号",
// 	"deptname":"部门名称",
// 	"leave_time":"离岗时间",
// 	"companyname":"创立公司名称",
// 	"belong_industry":"所属行业",
// 	"company_address":"公司地址",
// 	"company_valuation":"公司估值",
// 	"company_scale":"公司规模"
// }